<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'detail';
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择课程','error','jy_onlineedu/index/');
	}
	$this->session('id',$id);
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$id);
	if($_RQ['keyword']){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
	}
	$item=pdo_get('jy_onlineedu_video',$params);
	if($item['status']!=1 || $item['noshow']==1){
		exi('该课程已经关闭！','error','jy_onlineedu/index/');
	}
	$item['thumb']=tomedia($item['thumb']);
	$item['videos']=$item['videos']?json_decode($item['videos'],true):array();
	foreach($item['videos'] as &$vv){
		$vv['video']=tomedia($vv['video']);
		$vv['thumb']=tomedia($vv['thumb']);
	}
	$item['video']=$item['videos'][0]['video']?tomedia($item['videos'][0]['video']):'';
	//更新足迹
	pdo_update('jy_onlineedu_video',array('readnum +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$history=pdo_getcolumn('jy_onlineedu_video_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$id),'id');
	if($history){
		pdo_update('jy_onlineedu_video_log',array('createtime'=>time()),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$id));
	}else{
		pdo_insert('jy_onlineedu_video_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$id,'createtime'=>time()));
	}
	//判断是否点赞：
	$return['islike']=pdo_getcolumn('jy_onlineedu_video_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$id),'islike');
	//是否关注
	$return['isfollow']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');
	$return['isfans']=pdo_getcolumn('jy_onlineedu_userlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$item['mid']),'isfollow');
	//付费
	//是否付费
	$return['isvip']=$this->model('model')->checkvip();
	$item['noread']=false;
	if($item['type']>0 && $item['price']>0){
		if($item['type']==2 && !$return['isvip']){
			$item['noread']=true;
		}elseif($item['type']==1){
			$order=pdo_get('jy_onlineedu_video_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$item['id']));			
			if(!$order['id']){
				$item['noread']=true;
			}
			$order=pdo_get('jy_onlineedu_video_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$item['id']));	
			if($order['id']){
				$item['order_id']=$order['id'];
			}
		}
	}
	$return['item']=$item;
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);	
	if($item['video']){
		$params['video <>']='';
	}else{
		$params['video']='';
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_onlineedu_video',$params,array($pindex,$psize),$total,$fields,'','updatetime desc');
	foreach($list as &$kv){
		$item['thumb']=tomedia($item['thumb']);
	}
	$return['tuijievideos']=$list;
	//作者
	$user=pdo_get('jy_onlineedu_user',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
	//VIP时间：
	$user['vipstatus']=$user['viptime']>time()?1:2;
	if(($user['isvip'] !=1 || $user['viptime'] < time()) && $return['pluginset']['isfee_hongbao']){
		$user['ishongbao'] =0;
	}else{
		$user['ishongbao'] =1;
	}
	$return['user']=$user;
	$return['post']=array('nickname'=>pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'nickname'));
	$return['sharedata']=array(
		'title'=>$item['title']?$item['title']:'课程详情',
		'desc'=>$item['desc']?$item['desc']:'课程说明',
		'link'=>custom_url('jy_onlineedu/video_detail//id='.$id,true),
		'imgUrl'=>tomedia($item['thumb']),
	);
	set_html('title',$item['title']);
	$return['showtype']=0;
	exi($return);
}
if($_RQ['op']=='like'){
	$post=get_postdata();
	$id = $post['id'];
	$like=pdo_get('jy_onlineedu_video_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$id),array('id','islike'));
	if($like['id']){
			$islike=$like['islike']?0:1;
			pdo_update('jy_onlineedu_video_log',array('islike'=>$islike),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$id));
			exi($islike?'点赞成功':'取消成功');
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'islike'=>1,
			'video_id'=>$id,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_onlineedu_video_log',$insert);
		exi('点赞成功');
	}	
}
if($_RQ['op']=='follow'){
	$id = $this->session('id');
	if(!$id){
		exi('没有找到该作品，请重新选择！','error');
	}
	$opus=pdo_get('jy_onlineedu_video',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($opus['mid'] == $_SESSION['uid']){
		exi('自己不能关注自己！','error');
	}
	$follow=pdo_get('jy_onlineedu_userlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$opus['mid']));
	if($follow){
		if($follow['isfollow'] !=1){
			pdo_update('jy_onlineedu_userlog',array('isfollow'=>1,'createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$opus['mid']));
			exi('关注成功');
		}else{
			pdo_update('jy_onlineedu_userlog',array('isfollow'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'userid'=>$opus['mid']));
			exi('已取消关注');
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'userid'=>$opus['mid'],
			'mid'=>$_SESSION['uid'],
			'isfollow'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_onlineedu_userlog',$insert);
	}
	exi('关注成功');
}
if($_RQ['op']=='pay'){
	$postdata=get_postdata();
	$video_id = $postdata['video_id'];
	//判断待支付订单及判断是否超过可预订次数
	$article=pdo_get('jy_onlineedu_video',array('id'=>$video_id));
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'video_id'=>$video_id,
			'fee'=>$article['price'],
			'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_onlineedu_video_order',$insert);
	$id=pdo_insertid();
	$params=array(
					'id'=>$id,
					'title'=>'课程付费阅读',
					'fee'=>$article['price'],
					'plugin'=>'jy_onlineedu',
					//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
					'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
					'back_url'=>'jy_onlineedu/video_detail/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
					'success_url'=>'jy_onlineedu/video_detail/detail/id='.$video_id,
					'error_url'=>'jy_onlineedu/video_detail/detail/id='.$video_id,
	);
	//虚拟设置
	$plugin_xuni=$this->model('model')->plugin_xuni($insert);
	if(count($plugin_xuni)){
		$params['plugin_xuni']=$plugin_xuni;
	}
	$payid=cfc('pay')->create_pay_order($params);
	pdo_update('jy_onlineedu_video_order',array('pay_id'=>$payid),array('id'=>$id));
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$id,
	);
	exi($res);
}
if($_RQ['op']=='twopay'){
	$post=get_postdata();
	$id=$post['id'];
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$pay_id=pdo_getcolumn('jy_onlineedu_video_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'pay_id');
	exi($pay_id);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_onlineedu/video_detail/id='.$payresult['id']);
	}
	$order=pdo_get('jy_onlineedu_video_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_onlineedu/video_detail/id='.$payresult['id']);
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['fee'].'|'.$payresult['fee'],'error','jy_onlineedu/video_detail/id='.$payresult['id']);
	}
	pdo_update('jy_onlineedu_video_order',array('ispay'=>1),array('id'=>$payresult['id']));
	$order['price']=$order['fee'];
	$order['plugin_name']='jy_onlineedu';
	$opus=pdo_get('jy_onlineedu_video',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['video_id']));
	//计算极差分红
	if($opus['isfen']){
		model('jy_commission/jicha')->create($order);
	}
	exi('提交成功!');
}
if($_RQ['op']=='sendhb'){
	$id=$_RQ['id'];
	if(!$_RQ['id']){
		$id=$this->session('id');
	}
	if(!$id){
		exi('未正确获取课程id','error');
	}	
	$post=array();
	$post['money']=0;
	 $user=pdo_get('jy_onlineedu_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	 $opus=pdo_get('jy_onlineedu_video',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($_RQ['postdata']){
	   $postdata=get_postdata();
	   $money=$postdata['money'];
	   $username=$postdata['username'];
		if($money<=1){
		   exi('红包金额需大于1元','error');
	   }
	   if($money>200){
		   exi('单个红包金额最多不超过200元','error');
	   }
	   if(!$username){
		  // exi('请输入发送人姓名！','error');
	   }
	   //创建红包订单
	   $insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'oid'=>$id,
			'omid'=>$opus['mid'],
			'fee'=>$money,
			'ordersn'=>get_ordersn('jy_onlineedu_redorder','ordersn'),
			'username'=>$username,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP
	   );
	   pdo_insert('jy_onlineedu_redorder',$insert);
	   //支付
	   $id=pdo_insertid();
	   $params['fee']=$money;
	    $params['title']='发送红包订单';
		$params['id']=$id;
		$params['plugin']='jy_onlineedu';
		$params['pay_type']='wechat';//可选，此项作用为指定支付方式
		$params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
		$params['back_url']='jy_onlineedu/video_detail/redpayresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);
	}
	$return['post']=$post;
	exi($return);
}

if($_RQ['op']=='redpayresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_onlineedu_redorder',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！发送红包订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！发送红包订单金额不匹配！','error');
	}
	pdo_update('jy_onlineedu_redorder',array('ispay'=>1,'fee'=>$payresult['fee']),array('id'=>$order['id']));
	//更新发红包额
	pdo_update('jy_onlineedu_user',array('money_pay +='=>$order['fee']),array('mid'=>$_SESSION['uid']));
	//发红包
	//收红包人的信息
	$suser=pdo_get('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['omid']));
	//发送红包	
	if($return['pluginset']['isconfirm']){
		exi('发送成功');
	}
	$nickname = pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'nickname');
	$arr['openid'] = $suser['openid'];
	$arr['act_name'] = '商学院课程';
	$arr['remark'] = "您收到一个好友的鼓励红包";
	$arr['money'] = $order['fee'];
	$arr['mch_billno'] = $order['ordersn'];
	$arr['send_name'] = $order['username']?$order['username']:$nickname;
	$res = model('core/wechat_pay')->redpack($arr);
 	if($res['result_code'] == 'SUCCESS' && $res['return_code'] == 'SUCCESS'){
		pdo_update('jy_onlineedu_redorder',array('status'=>1),array('id'=>$order['id']));
		pdo_update('jy_onlineedu_opus',array('money +='=>$order['fee'],'red_num +='=>1),array('id'=>$order['oid']));
		//更新收红包额
		pdo_update('jy_onlineedu_user',array('money_get +='=>$order['fee']),array('mid'=>$order['omid']));
		exi('红包转发成功','success');
	}else{
		pdo_update('jy_onlineedu_user',array('cash +='=>$order['fee']),array('mid'=>$order['omid']));
		//更新发红包额
		pdo_update('jy_onlineedu_user',array('money_pay +='=>$order['fee']),array('mid'=>$_SESSION['uid']));
		exi('红包发送失败','error');
	}
}

?>